var checkbox = document.getElementById("sp-all"); /* 全选框 */
var warecheck = document.getElementsByClassName("checkbox"); /* 商品的选择框 */
var cartBox = document.querySelector(".all-cart-thing"); /* 所有商品div的父div */
var n = 1,
    mm = {},
    mn = []; /* 用于跟踪数据 */

/* 遍历本地数据 */
for (i = 0; i < localStorage.length; i++) {
    var localKey = localStorage.key(i);
    /* 从本地数据中提取储存的商品 */
    if (/^obj[0-9]*$/.test(localKey)) {
        /* 创建购物车商品框架 */
        var cartKj = (document.getElementById("hiddendiv").querySelector(".cart-thing")).cloneNode(true); /* 商品信息框架 */
        cartBox.appendChild(cartKj);
        /* 获取创建的相关元素 */
        var allware = document.querySelectorAll(".cart-thing"); /* 单个商品div */
        var img = allware[n].querySelector("img"); /* 商品图片 */
        var content = allware[n].querySelector(".spIF"); /* 商品信息 */
        var money = allware[n].querySelector(".spMoney"); /* 单价 */
        var wareIf = strtoobj(find(localKey)); /* 相应商品的对象 */
        /* 将商品信息填入框架 */
        img.src = wareIf.src;
        content.innerHTML = wareIf.text;
        money.innerHTML = wareIf.money;
        n++;
        /* 将本地数据顺序储存 */
        mm[i] = localStorage.key(i);
        mn.push(i);
    }
    var allware = document.querySelectorAll(".cart-thing");
    if (allware.length > 1) {
        /* 将商品价格(小计)填入框架 */
        var zhi = allware[0].querySelector(".spNum").querySelector(".inputNum");
        dmon(allware[i], zhi.value);
    }
}

/* 为商品绑定顺序,方便在本地数据中定位位置 */
for (i = 0; i < mn.length; i++) {
    allware[i + 1].setAttribute("date-name", mm[mn[i]]);
}

/* 检查商品数量，防止商品数量为数字且<=0 */
function checkWareNum(x) {
    if (x.value <= 0 || /^[0-9]$/.test(x.value == false)) {
        x.value = 1;
        x.parentNode.querySelector(".reduce").disabled = true;
    }
    /* 数量改变时 */
    var parent = x.parentNode;
    var zhi = parent.querySelector(".inputNum");
    if (zhi.value == 1) parent.querySelector(".reduce").disabled = true;
    else parent.querySelector(".reduce").disabled = false;
    var gparent = parent.parentNode.parentNode;
    dmon(gparent, zhi.value); /* 数量发生变化时，重新统计改商品的价格 */
    Smoney(); /* 数量、选择发生变化是，重新统计总价格 */
}

/* 单种商品价钱(单价*数量) */
function dmon(x, y) {
    var allmoney = x.querySelector(".spAllmoney");
    var money = x.querySelector(".spMoney");
    allmoney.innerHTML = "¥ " + (getnum(money.textContent) * y).toFixed(2);
}

/* 统计商品数量,价格 */
function Smoney() {
    var num = document.querySelector(".allspnum");
    var mon = document.querySelector(".allspmoney");
    var spmon = document.getElementsByClassName("spAllmoney");
    var s1 = 0,
        s2 = 0;
    for (i = 1; i < spmon.length; i++) {
        if (warecheck[i].checked == true) {
            s1 += getnum(spmon[i].textContent) - 0;
            s2++;
        }
    }
    for (i = 1; i < warecheck.length; i++) {

    }
    mon.innerHTML = "¥ " + s1.toFixed(2);
    num.innerHTML = s2;
}

/* 商品数量-1 */
function reduce() {
    var parent = this.parentNode;
    var zhi = parent.querySelector(".inputNum");
    var gparent = parent.parentNode.parentNode;
    zhi.value--;
    dmon(gparent, zhi.value);
    Smoney();
    if (zhi.value == 1) parent.querySelector(".reduce").disabled = true;
}

/* 商品数量+1 */
function add() {
    var parent = this.parentNode;
    parent.querySelector(".reduce").disabled = false;
    var zhi = parent.querySelector(".inputNum");
    zhi.value++;
    var gparent = parent.parentNode.parentNode;
    dmon(gparent, zhi.value);
    Smoney();
}

/* 全选 */
function qx() {
    if (checkbox.checked == false) {
        for (i = 1; i < warecheck.length; i++) {
            warecheck[i].checked = true;
            warecheck[i].parentNode.style.backgroundColor = "#fedede";
        }
    } else {
        for (i = 1; i < warecheck.length; i++) {
            warecheck[i].checked = false;
            warecheck[i].parentNode.style.backgroundColor = "white";
        }
    }
    Smoney();
}

/* 选择当前商品 */
function xz() {
    var parent = this.parentNode;
    if (this.checked == true) {
        parent.style.backgroundColor = "#fedede";
    } else {
        parent.style.backgroundColor = "white";
        checkbox.checked = false;
    }
    Smoney();
}

/* 删除当前商品 */
function delware(x) {
    var thisware = x.parentNode.parentNode;
    cartBox.removeChild(thisware);
    Smoney();
    var key = thisware.getAttribute("date-name");
    del(key);
}

/* 删除选中商品 */
function delxz() {
    /* 因为每次都要重新获取，因此不直接用warecheck，而是重新声明 */
    var nowwarecheck = document.querySelectorAll(".checkbox");
    var nowallware = document.querySelectorAll(".cart-thing");
    for (i = nowwarecheck.length - 1; i > 0; i--) {
        /* 选择选中的商品 */
        if (nowwarecheck[i].checked == true) {
            cartBox.removeChild(nowwarecheck[i].parentNode);
            var key = nowallware[i].getAttribute("date-name");
            del(key);
        }
    }
    Smoney();

    /* 用removeChild()删除全部节点要从后往前删，以下代码无法实现全部删除 */

    /* for (i = 1; i < warecheck.length; i++) {
        if (warecheck[i].checked == true) {
            cartBox.removeChild(warecheck[i].parentNode);
        }
    } */

    /* 参考文案https://blog.csdn.net/TheManOfCoding/article/details/78650863 */
}


/* 为加减（数量）绑定事件 */
var numreduce = document.querySelectorAll(".reduce");
var numadd = document.querySelectorAll(".add");
for (i = 0; i < numadd.length; i++) {
    numreduce[i].addEventListener("click", reduce);
    numadd[i].addEventListener("click", add);
}

/* 为选择框绑定事件 */
for (i = 1; i < warecheck.length; i++) {
    warecheck[i].addEventListener("click", xz)
}
Smoney(); /* 统计一次价格 */